package com.cham.video.controller;

import com.cham.enums.ChamSysCodeEnum;
import com.cham.utils.TokenUtils;
import com.cham.video.entity.ChamVideo;
import com.cham.video.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.nio.channels.FileChannel;
import java.util.Date;

/**
 * @author Howl-Xu
 * @version 1.0.0
 * @Description: 请描述一下这个类文件吧
 * @createTime 2020年03月15日 17:14:00
 */
@Controller
@RequestMapping("/tougaoController")
public class TougaoController {

    @Autowired
    private VideoService videoService;


    @PostMapping("/putongTougao")
    @ResponseBody
    @Transactional
    public ChamSysCodeEnum putongTougao(@RequestParam("videoCoverImg220x125") MultipartFile videoCoverImg220x125,
                                  @RequestParam("fileName") String fileName,
                                  @RequestParam("fenquParent") String videoFenquParent,
                                  @RequestParam("fenquChild") String videoFenquChild,//视频分区子字段
                                  @RequestParam("videoTitle") String videoTitle,//视频标题
                                  @RequestParam("videoSummary") String videoSummary,
                                  @RequestParam("fenzuId") int fenzuId,
                                        @RequestParam("videoTimeStr") String videoTimeStr
    ) throws IOException {
        String videoPath = "http://localhost:8082/cham/video/"+fileName;
        String imgPath = "http://localhost:8083/cham/img/";
        String imgFilePath = "D:/chamImgServer/";
        //保存圖片文件到服務器
        String fileName220x125 = new Date().getTime()+"_"+videoCoverImg220x125.getOriginalFilename();
        File newImgFile220x125 = new File(imgFilePath+fileName220x125);
        videoCoverImg220x125.transferTo(newImgFile220x125);

        ChamVideo chamVideo = new ChamVideo();
        chamVideo.setVideoName(fileName);
        chamVideo.setVideoPath(videoPath);
        chamVideo.setVideoTimeStr(videoTimeStr);
        //添加视频图片地址
        chamVideo.setVideoCoverImg220x125(imgPath+fileName220x125);

        /**
         * 添加uuid-s
         */
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String token = request.getHeader("token");
        String userUUID = TokenUtils.getLoginUserUUID(token);
        chamVideo.setUserUuid(userUUID);
        /**
         * 添加uuid-e
         */
        chamVideo.setVideoTitle(videoTitle);
        chamVideo.setVideoSummary(videoSummary);
        chamVideo.setCreateDate(new Date());
        //添加分区信息
        chamVideo.setFenquParent(videoFenquParent);
        chamVideo.setFenquChild(videoFenquChild);
        //添加附加信息
        chamVideo.setDianzan(0);
        chamVideo.setBofang(0);
        chamVideo.setToubi(0);

        //设置新番分组Id
        chamVideo.setFenzuId(fenzuId);
        int fenzuIndex = -1;
        if(fenzuId>0){
            fenzuIndex = videoService.getMaxFenzuIndexByFenzuId(fenzuId);
        }

        //设置分组index
        chamVideo.setFenzuIndex(fenzuIndex+1);

        videoService.saveVideoInfo(chamVideo);
        return ChamSysCodeEnum.TOUGAO_SUCCESS;
    }


    @PostMapping("/specialVideoTougao")
    @ResponseBody
    @Transactional
    public ChamSysCodeEnum specialVideoTougao(@RequestParam("videoCoverImg470x200") MultipartFile videoCoverImg470x200,
                                        @RequestParam("fileName") String fileName,
                                        @RequestParam("videoTitle") String videoTitle,//视频标题
                                        @RequestParam("videoSummary") String videoSummary,
                                              @RequestParam("videoTimeStr") String videoTimeStr
    ) throws IOException {
        String videoPath = "http://localhost:8082/cham/video/"+fileName;
        String imgPath = "http://localhost:8083/cham/img/";
        String imgFilePath = "D:/chamImgServer/";
        //保存圖片文件到服務器
        String fileName470x200 = new Date().getTime()+"_"+videoCoverImg470x200.getOriginalFilename();
        File newImgFile470x200 = new File(imgFilePath+fileName470x200);
        videoCoverImg470x200.transferTo(newImgFile470x200);

        ChamVideo chamVideo = new ChamVideo();
        chamVideo.setVideoName(fileName);
        chamVideo.setVideoPath(videoPath);
        chamVideo.setVideoTimeStr(videoTimeStr);
        //添加视频图片地址
        chamVideo.setVideoCoverImg470x200(imgPath+fileName470x200);

        /**
         * 添加uuid-s
         */
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String token = request.getHeader("token");
        String userUUID = TokenUtils.getLoginUserUUID(token);
        chamVideo.setUserUuid(userUUID);
        /**
         * 添加uuid-e
         */
        chamVideo.setVideoTitle(videoTitle);
        chamVideo.setVideoSummary(videoSummary);
        chamVideo.setCreateDate(new Date());

        chamVideo.setFenzuId(0);
        chamVideo.setFenzuIndex(0);

        videoService.saveSPVideoInfo(chamVideo);
        return ChamSysCodeEnum.TOUGAO_SUCCESS;
    }

    @PostMapping("/xinfanTougao")
    @ResponseBody
    @Transactional
    public ChamSysCodeEnum xinfanTougao(@RequestParam("videoCoverImg220x125") MultipartFile videoCoverImg220x125,
                                        @RequestParam("videoCoverImg80x80") MultipartFile videoCoverImg80x80,
                                        @RequestParam("fileName") String fileName,
                                        @RequestParam("fenquParent") String videoFenquParent,
                                        @RequestParam("fenquChild") String videoFenquChild,//视频分区子字段
                                        @RequestParam("videoTitle") String videoTitle,//视频标题
                                        @RequestParam("videoSummary") String videoSummary,
                                        @RequestParam("fenzuId") int fenzuId,
                                        @RequestParam("videoTimeStr") String videoTimeStr
    ) throws IOException {
        String videoPath = "http://localhost:8082/cham/video/"+fileName;
        String imgPath = "http://localhost:8083/cham/img/";
        String imgFilePath = "D:/chamImgServer/";
        //保存圖片文件到服務器
        String fileName220x125 = new Date().getTime()+"_"+videoCoverImg220x125.getOriginalFilename();
        File newImgFile220x125 = new File(imgFilePath+fileName220x125);
        videoCoverImg220x125.transferTo(newImgFile220x125);

        String fileName80x80 = new Date().getTime()+"_"+videoCoverImg80x80.getOriginalFilename();
        File newImgFile80x80 = new File(imgFilePath+fileName80x80);
        videoCoverImg80x80.transferTo(newImgFile80x80);

        ChamVideo chamVideo = new ChamVideo();
        chamVideo.setVideoName(fileName);
        chamVideo.setVideoPath(videoPath);
        chamVideo.setVideoTimeStr(videoTimeStr);
        //添加视频图片地址
        chamVideo.setVideoCoverImg220x125(imgPath+fileName220x125);
        chamVideo.setVideoCoverImg80x80(imgPath+fileName80x80);

        /**
         * 添加uuid-s
         */
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String token = request.getHeader("token");
        String userUUID = TokenUtils.getLoginUserUUID(token);
        chamVideo.setUserUuid(userUUID);
        /**
         * 添加uuid-e
         */
        chamVideo.setVideoTitle(videoTitle);
        chamVideo.setVideoSummary(videoSummary);
        chamVideo.setCreateDate(new Date());
        //添加分区信息
        chamVideo.setFenquParent(videoFenquParent);
        chamVideo.setFenquChild(videoFenquChild);
        //添加附加信息
        chamVideo.setDianzan(0);
        chamVideo.setBofang(0);
        chamVideo.setToubi(0);

        //设置新番分组Id
        chamVideo.setFenzuId(fenzuId);
        int fenzuIndex = videoService.getMaxFenzuIndexByFenzuId(fenzuId);


        //设置分组index
        chamVideo.setFenzuIndex(fenzuIndex+1);
        videoService.saveXinfanInfo(chamVideo);
        return ChamSysCodeEnum.TOUGAO_SUCCESS;
    }


//    @PostMapping("/tougao")
//    @ResponseBody
//    @Transactional
//
//    public ChamSysCodeEnum tougao(@RequestParam("videoCoverImg220x125") MultipartFile videoCoverImg220x125,
//                                    @RequestParam("videoCoverImg470x200") MultipartFile videoCoverImg470x200,
//                                    @RequestParam("videoCoverImg80x80") MultipartFile videoCoverImg80x80,
//                                    @RequestParam("fileName") String fileName,
//                                    @RequestParam("videoFenquParent") String videoFenquParent,
//                                    @RequestParam("videoFenquChild") String videoFenquChild,//视频分区子字段
//                                    @RequestParam("videoTitle") String videoTitle,//视频标题
//                                    @RequestParam("videoSummary") String videoSummary
//                                  ) throws IOException {
//
//        String videoPath = "http://localhost:8082/cham/video/"+fileName;
//
//        String imgPath = "http://localhost:8083/cham/img/";
//        String imgFilePath = "D:/chamImgServer/";
//
//        //保存圖片文件到服務器
//        String fileName220x125 = new Date().getTime()+"_"+videoCoverImg220x125.getOriginalFilename();
//        File newImgFile220x125 = new File(imgFilePath+fileName220x125);
//        videoCoverImg220x125.transferTo(newImgFile220x125);
//
////        String fileName325x246 = new Date().getTime()+"_"+videoCoverImg325x246.getOriginalFilename();
////        File newImgFile325x246 = new File(imgFilePath+fileName325x246);
////        videoCoverImg325x246.transferTo(newImgFile325x246);
//
//        String fileName470x200 = new Date().getTime()+"_"+videoCoverImg470x200.getOriginalFilename();
//        File newImgFile470x200 = new File(imgFilePath+fileName470x200);
//        videoCoverImg470x200.transferTo(newImgFile470x200);
//
//        String fileName80x80 = new Date().getTime()+"_"+videoCoverImg80x80.getOriginalFilename();
//        File newImgFile80x80 = new File(imgFilePath+fileName80x80);
//        videoCoverImg80x80.transferTo(newImgFile80x80);
//
//        ChamVideo chamVideo = new ChamVideo();
//        chamVideo.setVideoName(fileName);
//        chamVideo.setVideoPath(videoPath);
//
//        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        HttpServletRequest request = attributes.getRequest();
//        String token = request.getHeader("token");
//        String userUUID = TokenUtils.getLoginUserUUID(token);
//        chamVideo.setUserUuid(userUUID);
//        chamVideo.setVideoTitle(videoTitle);
//        chamVideo.setVideoSummary(videoSummary);
//        chamVideo.setCreateDate(new Date());
//
//        videoService.saveVideoInfo(chamVideo);
//        int videoId = chamVideo.getVideoId();
//
//        //保存封面图信息
//        ChamVideoCoverImg coverImg = new ChamVideoCoverImg();
//        coverImg.setVideoId(videoId);
//        coverImg.setVideoCoverImg220x125(imgPath+fileName220x125);
////        coverImg.setVideoCoverImg325x246(imgPath+fileName325x246);
//        coverImg.setVideoCoverImg470x200(imgPath+fileName470x200);
//        coverImg.setVideoCoverImg80x80(imgPath+fileName80x80);
//        coverImg.setUserUuid(userUUID);
//        videoService.saveChamVideoCoverImg(coverImg);
//
//
//
//
//
//
//
//
//        //保存分区信息
//        ChamVideoFenqu fenqu = new ChamVideoFenqu();
//        fenqu.setVideoId(videoId);
//        fenqu.setFenquParent(videoFenquParent);
//        fenqu.setFenquChild(videoFenquChild);
//        videoService.saveVideoFenqu(fenqu);
//
//
//        //保存附加信息逻辑
//        videoAppendInfo.setVideoId(videoId);
//        videoAppendInfo.setVideoBofang(0);
//        videoAppendInfo.setVideoDianzan(0);
//        videoAppendInfo.setVideoToubi(0);
//        videoService.saveVideoAppendInfo(videoAppendInfo);
//
//        return ChamSysCodeEnum.TOUGAO_SUCCESS;
//    }


    @PostMapping("/videoSection")
    @ResponseBody
    @Transactional
    //视频文件切片
//    public ChamSysCodeEnum videoSection(@RequestParam("file") MultipartFile file,
//                                  @RequestParam("uuid") String uuid) throws IOException {
    public ChamSysCodeEnum videoSection(@RequestParam("file") MultipartFile file,
                                      @RequestParam("uuid") String uuid) throws IOException {

        String videoFilePath = "D:/chamVideoSection/"+uuid;
//
//        //保存視頻文件到服務器
        //通过CommonsMultipartFile的方法直接写文件（注意这个时候）
        File newVideoFile=new File(videoFilePath);
        file.transferTo(newVideoFile);

        return ChamSysCodeEnum.TOUGAO_SUCCESS;
    }

    @PostMapping("/mergeVideo")
    @ResponseBody
    @Transactional
    public void mergeVideo(@RequestParam("uuidList") String[] uuidList,@RequestParam("fileName") String fileName){
        String resultPath = "D:/chamVideoServer/"+fileName;
        mergeFiles(uuidList,resultPath);
    }

    public static void mergeFiles(String[] uuidList, String resultPath) {
        File[] files = new File[uuidList.length];
        for (int i = 0; i < uuidList.length; i++) {
            files[i] = new File("D:/chamVideoSection/"+uuidList[i]);
        }

        File resultFile = new File(resultPath);

        try {
            FileChannel resultFileChannel = new FileOutputStream(resultFile, true).getChannel();
            for (int i = 0; i < uuidList.length; i++) {
                FileChannel blk = new FileInputStream(files[i]).getChannel();
                resultFileChannel.transferFrom(blk, resultFileChannel.size(), blk.size());
                blk.close();
            }
            resultFileChannel.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        for (int i = 0; i < uuidList.length; i++) {
            files[i].delete();
        }
    }
}
